var tableObj = {};
var oldTableObj = null;
var url = "";
var pagetotal = 0;
var pageCountUrl = "";
var pageSize = 8;
var startNubmer = 0;
var endNumber = 0;
var oldUrl = "";
var disPageId = "";
var tdVerticalAlign = false;
jQuery.extend({
	splitPage:function(tableObjArg,urlArg){
	 tdVerticalAlign = tableObjArg.verticalAlign;
	 disPageId = tableObjArg.disPageId;
	 disPageId = disPageId == null || disPageId == undefined || disPageId == "" ? "splitPageUl" : disPageId;
	 var disSplitPage1 = document.getElementById("disSplitPage" + disPageId);
	 disSplitPage1 = document.getElementById("disSplitPage" + disPageId);
	 var pageSizeObj = tableObjArg.pageSize;
	 if(pageSizeObj != null && pageSizeObj != undefined){
	    pageSize = pageSizeObj;
	 }
	 pageCountUrl = urlArg;
	 oldUrl = urlArg;
	 url = urlArg;
	 findDataCount();
	 tableObj = tableObjArg;
	 if(oldTableObj == null){
	    oldTableObj = tableObjArg;
	 }
	 var currentPageInput = document.getElementById("currentPageInput" + disPageId);
	 var tableDivId = tableObjArg.gridId;
	 if(currentPageInput == null || currentPageInput == undefined){
	    $("#" + tableDivId).after("<input id='currentPageInput"+disPageId+"' type='hidden' value='1'/>");
	 }
	 
	 if(disSplitPage1 == null || disSplitPage1 == undefined){
	    $("#" + tableDivId).after("<input id='disSplitPage"+disPageId+"' type='hidden' value='1'/>");
	 }
	 var pageDiv = document.getElementById("pageDiv" + disPageId);
	 if(pageDiv == null || pageDiv == undefined){
	    $("#" + tableDivId).after("<div id='pageDiv"+ disPageId +"' style='height:25px;' class='pagelist'><ul id="+disPageId+" style='width: 100%'></ul></div>");
	 }
	 var id = disPageId;
	 clickSplitPageNum();
    },
    
    // 绑定点击分页(num)
    clickSplitPage:function(num,pagetotal,curObj){
         findDataCount();
         disPageId = curObj.name;
    	 var currentPage = $("#splitPageA"+ disPageId + num).html();
    	  $("#currentPageInput" + disPageId).val(currentPage);
    	  
    	  // 点击分页判断要 显示的(num)
    	  if(num -5 > 0 && num + 5 <= pagetotal){
    		  $("#disSplitPage" + disPageId).val(num-5);
    	  }
    	  
    	 //点击分页判断要 显示的(num)
    	  if(num -5 > 0 && num + 5 > pagetotal){
    	      if(pagetotal - 10 > 0){
    		    $("#disSplitPage" + disPageId).val(pagetotal - 10);
    	      }else{
    	        $("#disSplitPage" + disPageId).val(1);
    	      }
    	  }
    	  
    	  if(num-5 <= 0){
    	      $("#disSplitPage" + disPageId).val(1);
    	  }
    	  clickSplitPageNum()
    },
    
    // 点上一页
    clickBeforePage:function(pagetotal,curObj){
         findDataCount();
         disPageId = curObj.name;
    	 var currentPage = $("#currentPageInput" + disPageId).val();// 获取当前页
    	 var beforeNum = eval(currentPage -1);// 获取上一页的(num)
    	 $("#currentPageInput" + disPageId).val(beforeNum);// 加入隐藏
    	 // 点击分页判断要 显示的(num)
    	  if(beforeNum -5 > 0 && beforeNum + 5 <= pagetotal){
    		  $("#disSplitPage" + disPageId).val(beforeNum-5);
    	  }
    	  
    	 //点击分页判断要 显示的(num)
    	  if(beforeNum -5 > 0 && beforeNum + 5 > pagetotal){
    		  $("#disSplitPage" + disPageId).val(pagetotal - 10);
    	  }
    	 clickSplitPageNum();
    },
    
    // 点下一页
    clickNextPage:function(pagetotal,curObj){
     disPageId = curObj.name;
     findDataCount();
     var currentPage = $("#currentPageInput" + disPageId).val();// 获取当前页
   	 var nextNum = parseInt(currentPage) + 1;// 获取下一页的(num)
   	 $("#currentPageInput" + disPageId).val(nextNum);// 加入隐藏
   	  // 点击分页判断要 显示的(num)
   	  if(nextNum -5 > 0 && nextNum + 5 <= pagetotal){
   		  $("#disSplitPage" + disPageId).val(nextNum-5);
   	  }
   	  
   	  //点击分页判断要 显示的(num)
   	  if(nextNum -5 > 0 && nextNum + 5 > pagetotal){
   		  $("#disSplitPage" + disPageId).val(pagetotal - 10);
   	  }
   	  if((+currentPage) < (+pagetotal)){
   	    //当前页小于或者等于总页
   	    clickSplitPageNum();
   	  }
   	 
    }
    
});
//点击分页的方法
function clickSplitPageNum(){
  block();
  var tableDivId = tableObj.gridId;
  var tableDivObj = $("#" + tableDivId);
  tableDivObj.html("");
  setPageArgs();
  var currenPageNumber =  $("#currentPageInput" + disPageId).val();
  currenPageNumber = currenPageNumber == null || currenPageNumber == undefined || currenPageNumber == "" ? "1" : currenPageNumber;
  url = oldUrl;
  if(url.indexOf("?") >= 0){
    url = url + "&pageVO.startNumber=" + startNumber + "&pageVO.endNumber=" + endNumber;
  }else{
    url = url + "?pageVO.startNumber=" + startNumber + "&pageVO.endNumber=" + endNumber;
  }
  var datas = findData(url);
  var dataStore = new Ext.data.JsonStore({data:datas,fields:tableObj.fields});
  var colM = tableObj.colM;
  var tableTile = "";
  var title = tableObj.title;
  if(title != null && title != undefined){
    tableTile = title;
  }
   var gridStore = new Ext.data.JsonStore({data: datas,fields:tableObj.fields});
   //说明表格是可编辑表格
   var gridPanel = new Ext.grid.EditorGridPanel({
      renderTo:tableObj.gridId,
      border: false,
      title:tableTile,
      height: tableObj.height,
      layout: 'fit',
      cm:colM,
      store:gridStore,
      viewConfig : { 
		forceFit : true, 
		scrollOffset : 0 
	  },
      clicksToEdit: 1
     });
     if(tdVerticalAlign){
	     setTdToVerticalAlign();
     }
     var id = disPageId;
	 var a = "<a class='"+oldUrl+"' name='"+disPageId+"' id='beforeA"+disPageId+"' onclick='$.clickBeforePage("+pagetotal+",this)'>上一页</a>";
	    var currentPage = $("#currentPageInput" + disPageId).val();// 当前页
	    var disSplitPageObj = $("#disSplitPage" + disPageId);
	    var disSplitPageDoc = disSplitPageObj.get(0);
	    var disSplitPage = parseInt($.trim(disSplitPageObj.val()));// 呈现分页
	    if(disSplitPageDoc == null || disSplitPageDoc == undefined){
	      disSplitPage = 1;
	    }
	    if((+pagetotal) > 1){
		    for(var j = disSplitPage; j <= pagetotal; j ++){
		    	// pagetotal小于或等于10显示
		    	if(j > 0 && j <= 10 + disSplitPage){
		    		a = a + "<a class='"+oldUrl+"' name='"+disPageId+"'id=splitPageA"+ disPageId + j + " onclick='$.clickSplitPage("+ j +"," + pagetotal + ",this)'>"+ j +"</a>";
		    	}
		    	// pagetotal大于10显示
		    	if(j > 10 + disSplitPage){
		    		for(var k = 10 + disSplitPage; k <= pagetotal; k ++){
		    			$("#splitPageA"+disPageId+k).hide();
		    		}
		    	}
		    }
	    }
	    a = a + "<a class='"+oldUrl+"' name='"+disPageId+"' id='afterA"+disPageId+"' onclick='$.clickNextPage("+pagetotal+",this)'>下一页</a>";
	    $("#" + id).html("");
	    $("#" + id).html(a);
	    
	    // 隐藏(上一页)
	    if(currentPage == "1"){
	    	$("#beforeA" + disPageId).hide();
	    	$("#splitPageA" +disPageId+ currentPage).attr("class","current");
	    }else{
	    	$("#beforeA" + disPageId).show();
	    	$("#splitPageA"+ disPageId + currentPage).attr("class","current");
	    }
	    
	    // 隐藏(下一页)
	    if(currentPage == pagetotal){
	    	$("#afterA" + disPageId).hide();
	    	$("#splitPageA"+ disPageId + currentPage).attr("class","current");
	    }else{
	    	if(pagetotal != 0){
	    		$("#afterA" + disPageId).show();
	    		$("#splitPageA" + disPageId + currentPage).attr("class","current");
	    	}else{
	    		$("#afterA" + disPageId).hide();
	    	}
	    }
	    unblock();
}


//查找表格数据
function findData(requestUrl){
    var data = null;
    $.ajax({
      type:"POST",
      url:requestUrl,
      async: false,
      success: function(result){
        data = strToJson(result);
        showSystemErrorMsg(result);
      },
      error: function(request,error,status){
         showErrorMsg(error);
      }
   });
   return data;
}
//查找记录总数
function findDataCount(){
   if(pageCountUrl.indexOf("Count.action") < 0){
	   pageCountUrl = pageCountUrl.replace(".action","Count.action");
   }
   var count = 0;
    $.ajax({
      type:"GET",
      url:pageCountUrl,
      async: false,
      cache: false,
      success: function(result){
        count = result;
        pagetotal = count % pageSize == 0 ? wholeDivision(count,pageSize) : wholeDivision(count,pageSize) + 1; 
        showSystemErrorMsg(result);
      },
      error: function(request,error,status){
         showErrorMsg(error);
      }
   });
   count = count == null || count == undefined || count == "" ? "0" : count;
   return count;
}
//整除方法
function wholeDivision(data1,data2){
    var result = (+data1) / (+data2);
    if(result >= 0){
      result = Math.floor(result);
    }else{
      result = Math.ceil(result);
    }
    return result;
}
//设置分页参数
function setPageArgs(){
  var currenPageNumber =  $("#currentPageInput" + disPageId).val();
  currenPageNumber = currenPageNumber == null || currenPageNumber == undefined || currenPageNumber == "" ? "1" : currenPageNumber;
  startNumber = ((+currenPageNumber) - 1) * pageSize + 1;
  endNumber = (+currenPageNumber) * pageSize;
}
//重新设置url
function renewUrl(urlArg){
   url  = urlArg;
   pageCountUrl = urlArg;
   oldUrl = urlArg;
   tableObj = oldTableObj;
}
